//
//  ActivityViewController.m
//  豆瓣
//
//  Created by Roach on 16/6/23.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "ActivityViewController.h"
#import "ActivityTableViewCell.h"
#import "ActivityModel.h"
#import "ActiVityDetailsViewController.h"

@interface ActivityViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *modelArray;

@end

@implementation ActivityViewController

- (void)viewDidLoad {
    
    [self config];
    [self values];
    [self creat];
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)config {
    
    self.modelArray = [[NSMutableArray alloc]init];
    
    self.navigationItem.title = @"活动";
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:103 / 255.0 green:155 / 255.0 blue:172 / 255.0 alpha:1];
    
}

- (void)values {
    
    NSString *path = [[NSBundle mainBundle]pathForResource:@"activitylist" ofType:@"txt"];
    
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    NSError *error = nil;
    
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    
    NSArray *duanziArray = [dictionary objectForKey:@"events"];
    
    for (NSDictionary *dic in duanziArray) {
        
        ActivityModel *model = [[ActivityModel alloc]init];
        [model setValuesForKeysWithDictionary:dic];
        [self.modelArray addObject:model];
        [model release];
        
    }
    
}

- (void)creat {
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - 64)];
    
    [self.view addSubview:_tableView];
    
    [_tableView release];
    
    self.tableView.rowHeight = 200;
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [_tableView registerClass:[ActivityTableViewCell class] forCellReuseIdentifier:@"cell"];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.modelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ActivityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    

    
    ActivityModel *model = [self.modelArray objectAtIndex:indexPath.row];
    
    cell.cellModel = model;
    
//    cell.backgroundColor = [UIColor redColor];
    
    return cell;
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
        ActiVityDetailsViewController *activityDetail = [[ActiVityDetailsViewController alloc]init];
    
        activityDetail.activityModel = [self.modelArray objectAtIndex:indexPath.row];
    
        [self.navigationController pushViewController:activityDetail animated:YES];
    
        [activityDetail release];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
